package ch.supsi.parentalcontrol;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class AdminPanel extends JPanel{
	
	// dichiaro gli elementi che utilizzo per realizzare la grafica
	private GridBagConstraints containerLayout = new GridBagConstraints();
	
	// elementi scheda
	private JButton addInBlack, addInWhite, addObjectOk;
	private JLabel whiteLabel, blackLabel, addObject;
	private JTextField newObject;
	private JList whiteList, blackList;
	private JScrollPane whiteScroll, blackScroll;
	private DefaultListModel whiteListModel;
	private DefaultListModel blackListModel;
	
	private String type;
	
	//private List<Object> realWhiteList = new ArrayList<Object>();
	//private List<Object> realBlackList = new ArrayList<Object>();
	
	
	// devo dichiarare le due liste da inserire nel JList
	
	/*public AdminPanel(Object[] newWhiteList, Object[] newBlackList){
		for(int i=0; i<newWhiteList.length; i++){
			realWhiteList.add(newWhiteList[i]);
		}
		
		for(int i=0; i<newBlackList.length; i++){
			realBlackList.add(newBlackList[i]);
		}
		
		initComponent();
	}*/
	
	public AdminPanel(DefaultListModel whiteModel, DefaultListModel blackModel, String type){		
		this.whiteListModel = whiteModel;		
		this.blackListModel = blackModel;
		this.type = type; 
		initComponent();
	}
	
	private void initComponent(){
	//public void initComponent(DefaultListModel whiteModel, DefaultListModel blackModel){
        addInBlack = new JButton();
        addInWhite = new JButton();
        addObjectOk = new JButton();
        whiteLabel = new JLabel();
        blackLabel = new JLabel();
        addObject = new JLabel();
        newObject = new JTextField();
        whiteList = new JList();
        blackList = new JList();
        whiteScroll = new JScrollPane();
        blackScroll = new JScrollPane();
        
        // setto il layout al pannello del this
        GridBagLayout thisLayout = new GridBagLayout();
        this.setLayout(thisLayout);
        
        whiteLabel.setText("White List");
        containerLayout.gridx = 0;
        containerLayout.gridy = 0;
        this.add(whiteLabel, containerLayout);

        blackLabel.setText("Black List");
        containerLayout.gridx = 2;
        containerLayout.gridy = 0;
        this.add(blackLabel, containerLayout);

        addInBlack.setText(">>");
        addInBlack.addActionListener(new GestioneSchedaAdmin(this));
        //containerLayout.gridx = 1;
        //containerLayout.gridy = 2;
        //this.add(addInBlack, containerLayout);

        addInWhite.setText("<<");
        addInWhite.addActionListener(new GestioneSchedaAdmin(this));
        //containerLayout.gridx = 1;
        //containerLayout.gridy = 2;
        //this.add(addInWhite, containerLayout);
        
        JPanel jp_addButtons = new JPanel();
        jp_addButtons.setLayout(new GridLayout(2,1));
        jp_addButtons.add(addInBlack);
        jp_addButtons.add(addInWhite);
        containerLayout.gridx = 1;
        containerLayout.gridy = 2;
        this.add(jp_addButtons, containerLayout);
        
        // definisco le white/black list
        /*whiteListModel = new DefaultListModel();
        for(int i=0; i<realWhiteList.size(); i++){
        	whiteListModel.addElement(realWhiteList.get(i));
        }*/
        whiteList.setModel(whiteListModel);
        whiteScroll.setViewportView(whiteList);
        whiteScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        whiteScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        whiteScroll.setPreferredSize(new Dimension(200,300));
        containerLayout.gridx = 0;
        containerLayout.gridy = 2;
        this.add(whiteScroll, containerLayout);

        /*blackListModel = new DefaultListModel();
        for(int i=0; i<realBlackList.size(); i++){
        	blackListModel.addElement(realBlackList.get(i));
        }*/
        blackList.setModel(blackListModel);
        blackScroll.setViewportView(blackList);
        blackScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        blackScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        blackScroll.setPreferredSize(new Dimension(200,300));
        containerLayout.gridx = 2;
        containerLayout.gridy = 2;
        this.add(blackScroll, containerLayout);
        
        addObject.setText("Name");
        //containerLayout.gridx = 0;
        //containerLayout.gridy = 4;
        //this.add(addObject, containerLayout);

        newObject.setText("");
        newObject.setPreferredSize(new Dimension(100, 30));
        //containerLayout.gridx = 1;
        //containerLayout.gridy = 4;
        //this.add(newObject, containerLayout);
        
        addObjectOk.setText("New Element");
        addObjectOk.addActionListener(new GestioneSchedaAdmin(this));
        //containerLayout.gridx = 2;
        //containerLayout.gridy = 4;
        //this.add(addObjectOk, containerLayout);

        //spazio vuoto
        JPanel jp_empty = new JPanel();
        Border empty = BorderFactory.createEmptyBorder();
        containerLayout.gridx = 0;
        containerLayout.gridy = 5;
        jp_empty.setBorder(empty);
        this.add(jp_empty, containerLayout);
        
        JPanel jp_addNewObj = new JPanel();
        jp_addNewObj.setLayout(new FlowLayout());
        //jp_addNewObj.add(addObject);
        //jp_addNewObj.add(newObject);
        jp_addNewObj.add(addObjectOk);
        //TitledBorder border = BorderFactory.createTitledBorder("New Object");
        //jp_addNewObj.setBorder(border);
        containerLayout.gridwidth = 3;
        containerLayout.gridx = 0;
        containerLayout.gridy = 6;
        this.add(jp_addNewObj, containerLayout);
        
	}

	public GridBagConstraints getContainerLayout() {
		return containerLayout;
	}

	public JButton getAddInBlack() {
		return addInBlack;
	}

	public JButton getAddInWhite() {
		return addInWhite;
	}

	public JButton getAddObjectOk() {
		return addObjectOk;
	}

	public JLabel getAddObject() {
		return addObject;
	}

	public JTextField getNewObject() {
		return newObject;
	}

	public JList getWhiteList() {
		return whiteList;
	}
	
	/*public void setWhiteList(List<Object> list) {
		//prima svuoto la lista
		this.whiteListModel.clear();
		
		//riempio la lista con valori dell'utente e popolo l'oggetto di lista
		for(int i=0; i<list.size(); i++){
			this.whiteListModel.addElement(list.get(i));
		}
		
		// applico le modifice alla lista
		this.whiteList.setModel(this.whiteListModel);
	}*/
	
	public JList getBlackList() {
		return blackList;
	}
	
	public void addWhiteObject(int indiceBlack){
		whiteListModel.addElement(blackListModel.getElementAt(indiceBlack));
		blackListModel.removeElementAt(indiceBlack);

		/*realWhiteList.add(realBlackList.get(indiceBlack));
		realBlackList.remove(indiceBlack);
		blackListModel.removeElementAt(indiceBlack);*/
	}
	
	public void addBlackObject(int indiceWhite){
		blackListModel.addElement(whiteListModel.getElementAt(indiceWhite));
		whiteListModel.removeElementAt(indiceWhite);

		/*realBlackList.add(realWhiteList.get(indiceWhite));
		realWhiteList.remove(indiceWhite);
		whiteListModel.removeElementAt(indiceWhite);*/
	}
		
	/*public void setBlackList(List<Object> list) {
		//prima svuoto la lista
		this.blackListModel.clear();
		
		//riempio la lista con valori dell'utente e popolo l'oggetto di lista
		for(int i=0; i<list.size(); i++){
			this.blackListModel.addElement(list.get(i));
		}
		
		// applico le modifice alla lista
		this.blackList.setModel(this.blackListModel);
	}*/

	public JScrollPane getWhiteScroll() {
		return whiteScroll;
	}

	public JScrollPane getBlackScroll() {
		return blackScroll;
	}

	public DefaultListModel getWhiteListModel() {
		return whiteListModel;
	}

	public DefaultListModel getBlackListModel() {
		return blackListModel;
	}
	
	public String getType(){
		return this.type;
	}
	
}
